## ogg文件辅助类
class_name OggHelper

#region const
## ogg文件标识：前四个字节为“oggS”
static var OGG_FILE_SIGN_BUFFER: PackedByteArray = PackedByteArray([79, 103, 103, 83])
## Theora视频流的标识符 "\x80theora"
static var OGG_THEORA_SIGN_BUFFER: PackedByteArray = PackedByteArray([128, 116, 104, 101, 111, 114, 97])
## 最大搜索范围16KB
const MAX_SEARCH_SIZE: int = 1024 * 16
#endregion const

#region IsOggFileContainsVideo
## 判断ogg文件是否包含视频流标识
## 参数：
## filePath-ogg文件路径名
## 返回：返回文件是否包含视频流。
static func IsOggFileContainsVideo(filePath: String, searchSize: int = MAX_SEARCH_SIZE) -> bool:
	var file: FileAccess = FileAccess.open(filePath, FileAccess.READ)
	if file == null:
		return false
	var buffer: PackedByteArray = file.get_buffer(searchSize)
	file.close()
	return IsOggBufferContainsVideo(buffer)
#endregion IsOggFileContainsVideo

#region IsOggBufferContainsVideo
## 判断ogg文件缓冲区中是否包含视频流标识
## 参数：
## buffer-ogg文件缓冲区
## 返回：返回缓冲区是否包含视频流。
static func IsOggBufferContainsVideo(buffer: PackedByteArray) -> bool:
	# 判断文件标识
	var oggFileSighLength: int = OGG_FILE_SIGN_BUFFER.size()
	if not buffer or buffer.size() < oggFileSighLength:
		return false
	var header = buffer.slice(0, oggFileSighLength)
	if header != OGG_FILE_SIGN_BUFFER:
		return false
	# 判断是否包含视频流标识
	return ArrayUtility.FindSubarray(buffer, OGG_THEORA_SIGN_BUFFER) != -1
#endregion IsOggBufferContainsVideo
